package Schedulers;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import Processes.Processes;

import threadedSimulator.Cpu;

//The scheduler will be called each time the CPU receives a system timer 
//interrupt or a process makes a system call. It will determine what event 
//activated it and then determine which runnable process to execute next 
//(don't forget about the context switch). You should implement schedulers 
//		for the round-robin, priority, and lottery scheduling policies.

public abstract class Scheduler implements SchedulerInterface {

	protected Cpu my_cpu;
	private List<Runnable> my_processes;
	List<Processes> my_list_of_processes = new LinkedList<Processes>();
	List<Processes> my_log = new LinkedList<Processes>();

	Scheduler(Cpu the_cpu) {
		my_cpu = the_cpu;
//		my_scheduled_threads.setMaxPriority(10);
		
		my_processes = new LinkedList<Runnable>();
	}

	public boolean systemCall(Processes the_interuptor) {
		System.out.println(the_interuptor.getName()
				+ " has requested a system call");
		return false;
	}
	
    public void quantumStep()
    {
    	haltCurrentProcess();
    }

	public boolean canRun(Thread the_input) {
//		boolean result = false;
//		if (my_running_queue.isEmpty()) {
//			my_running_queue.add(the_input);
//			result = true;
//		} else {
//			if (getPriority(the_input) != 10) {
//				result = false;
//			} else {
//				result = true;
//			}
//		}
		return true;
	}
	
	/**
	 * String representation of this RoundRobinScheduler.
	 */
	@Override
	public String toString() {
		StringBuilder b = new StringBuilder();
		b.append("Scheduler: Running process " + my_list_of_processes.size() +" "+ my_list_of_processes.get(0).getName() + " next.\n");
		
		for (Processes the_process: my_list_of_processes) {
			if(!the_process.can_run)
			{
				b.append(the_process.getName() + "' state: BLOCKED\n");
			}
			else if(the_process.running)
			{
				b.append(the_process.getName() + "'state: RUNNING\n");
			}
			else
			{
				b.append(the_process.getName() + "'state: RUNNABLE\n");
			}
		}
		
		b.append("...............");
		return b.toString();
	}

}
